<template>
  <div class="settings">
    <van-nav-bar safe-area-inset-top placeholder :title="$t('Setting')" left-arrow fixed @click-left="onClickLeft" />
    <van-pull-refresh v-model="isLoading" :pulling-text="$t('PullingText')" :loosing-text="$t('PullingText')" :success-text="$t('refreshSuccee')" @refresh="onRefresh">
      <van-collapse class="marginTop15" v-model="activeNames">
        <van-collapse-item :title="$t('SystemONOFF')" name="1">
          <languagePTVue :columns="toggleArr" :setValue="LocalArr.SystemSwitchValue" :reg="47"></languagePTVue>
        </van-collapse-item>
        <van-collapse-item :title="$t('WorkMode')" name="2">
          <languagePTVue
            :columns="solarModeArr"
            :reg="28"
            :setValue="LocalArr.SolarModeCode"
            @getFormData="setSolarMode"
          ></languagePTVue>
          <languagePTVue :setName="$t('ManualMode')" :columns="manualArr" :setValue="LocalArr.manualValue" :reg="36" v-if="isManual"></languagePTVue>
        </van-collapse-item>
      </van-collapse>
      <van-cell style="text-align:left" :title="$t('Setting')" is-link @click="pwdShow=true" />
      <van-dialog @confirm="pwdjumps" v-model="pwdShow" :title="$t('UnlockPwd')" :confirmButtonText="$t('OK')" :cancelButtonText="$t('Cancel')" width="200" show-cancel-button>
        <van-field :placeholder="$t('Password')" clearable v-model="pwd" type="digit" />
      </van-dialog>
    </van-pull-refresh>
  </div>
</template>

<script>
import languagePTVue from '../../../components/Language/LanguagePT'

export default {
  components:{
    languagePTVue
  },
  data(){
    return {
      isLoading:false,
      activeNames:[],
      toggleArr:[this.$t('Off'),this.$t('ON')],
      solarModeArr:[this.$t('SelfUse'),this.$t('FeedInPriority'),this.$t('BackupMode'),this.$t('Manual')],
      isManual:false,
      manualArr:[this.$t('StopChrgDischrg'),this.$t('ForcedCharge'),this.$t('ForcedDischarge')],
      pwdShow:false,
      pwd:'',
      basicPwd:'',
      advancedPwd:'',
      LocalArr:{
        SystemSwitchValue:'',
        SolarModeCode:'',
        manualValue:''
      }
    }
  },
  methods: {
    onClickLeft(){
      this.$router.push({path:sessionStorage.getItem('setFrom')});
    },
    async pwdjumps(){
      if(this.pwd==''){
        this.comm.tipsShow('PwdNotEmpty');
      }else{
        this.comm.loadingShow();
        let formData=[{"reg":0,"val":this.pwd}];
        let data=await this.SETAPI.setInvMachinePT(localStorage.getItem('domain'), {
          'optType':'setReg',
          'sn':localStorage.getItem('wifiSN'),
          'tokenId':localStorage.getItem('tokenId'),
          'num':1,
          'Data':JSON.stringify(formData)
        });
        if (data.success) {
          if(this.pwd==this.comm.showPwd(this.basicPwd)){
            sessionStorage.setItem('setOption',1);
            this.$router.push({path:'/x1hybridg4basic'});
          }else if(this.pwd==this.comm.showPwd(this.advancedPwd)){
            sessionStorage.setItem('setOption',2);
            this.$router.push({path:'/x1hybridg4setlist'});
          }else if(this.pwd=='6868'){
            sessionStorage.setItem('setOption',3);
            this.$router.push({path:'/x1hybridg4setlist'});
          }else{
            this.comm.tipsShow('UnlockFaild');
          }
        }else {
          this.comm.tipsShow('UnlockFaild');
        }
      }
    },
    async getData(){
      this.comm.loadingShow();
      let datas=await this.SETAPI.setGetDataPT(localStorage.getItem('domain'),{'tokenId':localStorage.getItem('tokenId'),'sn':localStorage.getItem('wifiSN')});
      if(datas.result!=null&&datas.result!="tokenId不合法!"){
        let dataArr = datas.result;
        this.LocalArr.SystemSwitchValue=dataArr[46];
        this.LocalArr.SolarModeCode=dataArr[27];
        /*判断用户的选择*/
        if(this.LocalArr.SolarModeCode==3){
          this.isManual=true;
        }else{
          this.isManual=false;
        }
        this.LocalArr.manualValue=dataArr[35];
        //进入设置页面判断
        this.basicPwd=dataArr[78];
        this.advancedPwd=dataArr[79];
      }else{
        this.comm.tipsShow('InvNoResponse');
      }
    },
    //设置SolarMode
    setSolarMode(data){
      /*判断用户的选择*/
      if(data==3){
        this.isManual=true;
      }else{
        this.isManual=false;
      }
    },
    onRefresh(){
      this.isLoading = false;
      for(let i in this.LocalArr){
        this.LocalArr[i]='';
      }
      this.getData();
    }
  },
  mounted(){
    this.getData();
  }
}
</script>

<style scoped lang="less">

</style>
